import {defineStore} from "pinia";
import {
    homeStoreType,
    itemStyle1TypeSczt,
    itemStyle1TypeYsx,
    itemStyle6Type,
    tableType
} from "@/store/modules/home/type";
import {getHomeDataYSx, getScxtDayStsData} from "@/api/home";
import {timestampToYesterDay} from "@/utils/dateUtilsT";

let useHomeStore = defineStore('useHome', {
    state(): homeStoreType {
        return {
            platformId: 0,
            platformOptions: [
                {
                    label: '粤省心平台',
                    value: 0,
                },
                {
                    label: '市场主体诉求平台',
                    value: 1
                },
            ],
            loading: false,
            ysxResult: {},
            scztResult: {},
            ysx: {
                hwTableData: [],
                hwTableHeader: [
                    {
                        label: '序号',
                        prop: 'index',
                    },
                    {
                        label: '热线号码',
                        prop: 'hotline',
                    },
                    {
                        label: '热线名称',
                        prop: 'name',
                        width: '150'
                    },
                    {
                        label: '接通率',
                        prop: 'jtl',
                    }
                ],
                callBackInfoItem: [
                    {
                        index: 1,
                        label: '12345回访总量（条）',
                        value: 'total',
                    },
                    {
                        index: 2,
                        label: '智能回访量（条）',
                        value: 'smartCallNum',
                    },
                    {
                        index: 3,
                        label: '人工回访量（条）',
                        value: 'agentCallNum',
                    },
                    {
                        index: 4,
                        label: '回访成功量（条）',
                        value: 'successNum',
                    },
                    {
                        index: 5,
                        label: '回访失败量（条）',
                        value: 'failNum',
                    },
                    {
                        index: 6,
                        label: '回访成功率（%）',
                        value: 'successRate',
                    },
                    {
                        index: 7,
                        label: '主动评价率（%）',
                        value: 'activePjRate',
                    },
                    {
                        index: 8,
                        label: '回访满意量（条）',
                        value: 'myNum',
                    },
                    {
                        index: 9,
                        label: '回访不满意量（条）',
                        value: 'bmyNum',
                    },
                    {
                        index: 10,
                        label: '回访满意率（%）',
                        value: 'myRate',
                    },
                ],
                otherTbale: {
                    overTimeNotBjInfo: {} as tableType,
                    overTimeNotReplyInfo: {} as tableType,
                    backEventInfo: {} as tableType,
                    qcInfo: {} as tableType,
                    callBackInfo: {} as tableType,
                    hardwareInfo: {} as tableType,
                    systemInfo: {} as tableType,
                }
            },
            sczt: {
                otherTbale: {
                    hotEventRankInfo: {} as tableType,
                    bmyEventInfo: {} as tableType
                }
            },
            itemStyle1Data: [],
            itemStyle2Data: {},
            itemStyle3Data: [],
            itemStyle4Data: [],
            itemStyle6Data: {} as itemStyle6Type,
            date: timestampToYesterDay()
        }
    },
    actions: {
        async useGetHomeDataYSx() {
            try {
                this.loading = true
                let res = await getHomeDataYSx({date: this.date})
                this.ysxResult = res.data
                this.changePlatform()
            } catch (err) {
                this.loading = false
            }
        },
        async useGetScxtDayStsData() {
            try {
                this.loading = true
                let res = await getScxtDayStsData({date: this.date})
                this.scztResult = res.data
                this.changePlatform()
            } catch (err) {
                this.loading = false
            }
        },
        changePlatform() {
            if (this.platformId == 0) {
                this.itemStyle1Data = this.ysxResult.areaEventAcceptInfo.itemList
                this.itemStyle2Data = this.ysxResult.platformRunInfo
                this.ysx.hwTableData = this.ysxResult.hotlineInfo.itemList
                this.ysx.hwTableData.forEach((item, index) => {
                    item.index = index + 1
                })
                this.itemStyle3Data = this.ysxResult.eventBjInfo.itemList
                this.itemStyle4Data = this.ysxResult.eventTypeInfo.itemList
                this.itemStyle6Data = this.ysxResult.agentInfo
                for (let i in this.ysx.otherTbale) {
                    this.ysx.otherTbale[i] = this.ysxResult[i]
                    if (i === 'callBackInfo') {
                        this.ysx.otherTbale.callBackInfo.dataList = []
                        this.ysx.callBackInfoItem.forEach((item) => {
                            console.log(this.ysxResult[i],item.value)
                            this.ysx.otherTbale.callBackInfo.dataList.push(
                                {
                                    '数据指标': item.label,
                                    '数值': this.ysxResult[i][item.value]
                                }
                            )
                        })
                        this.ysx.otherTbale.callBackInfo.headers = ['数据指标', '数值']
                    }
                    if (this.ysx.otherTbale[i].dataList.length > 0 || this.ysx.otherTbale[i].headers.length > 0) {
                        this.ysx.otherTbale[i].dataList.forEach((item: any, index: number) => {
                            item['序号'] = index + 1
                        })
                        if (this.ysx.otherTbale[i].headers.indexOf('序号') == -1) {
                            this.ysx.otherTbale[i].headers.unshift('序号')
                        }
                    }
                }
            } else {
                this.itemStyle1Data = this.scztResult.areaEventInfo.itemList
                this.itemStyle2Data = this.scztResult.platformInfo
                this.itemStyle3Data = this.scztResult.eventMonthNumInfo.itemList
                this.itemStyle4Data = this.scztResult.eventTypeInfo.itemList
                for (let i in this.sczt.otherTbale) {
                    this.sczt.otherTbale[i] = this.scztResult[i]
                    console.log(this.sczt.otherTbale[i].dataList)
                    if (this.sczt.otherTbale[i].dataList.length > 0 || this.sczt.otherTbale[i].headers.length > 0) {
                        this.sczt.otherTbale[i].dataList.forEach((item: any, index: number) => {
                            item['序号'] = index + 1
                        })
                        if (this.sczt.otherTbale[i].headers.indexOf('序号') == -1) {
                            this.sczt.otherTbale[i].headers.unshift('序号')
                        }
                    }
                }

            }

        }
    }
})
export default useHomeStore
